package com.example.teachingSystem.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.teachingSystem.mapper.*;
import com.example.teachingSystem.pojo.entity.*;
import com.example.teachingSystem.pojo.form.exam.AddExamForm;
import com.example.teachingSystem.pojo.form.exam.ExamPageQuery;
import com.example.teachingSystem.pojo.form.exam.UpdateExamForm;
import com.example.teachingSystem.pojo.vo.ExamVo;
import com.example.teachingSystem.service.ExamService;
import com.example.teachingSystem.utils.JwtHelper;
import com.example.teachingSystem.utils.result.PageResult;
import com.example.teachingSystem.utils.result.Result;
import com.example.teachingSystem.utils.result.ResultCodeEnum;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author 86147
 * @description 针对表【exam(考试表)】的数据库操作Service实现
 * @createDate 2023-11-27 21:43:54
 */
@Service
public class ExamServiceImpl extends ServiceImpl<ExamMapper, Exam>
        implements ExamService {

    @Autowired
    private ExamMapper examMapper;
    @Autowired
    private StudentMapper studentMapper;

    @Autowired
    private TeacherMapper teacherMapper;

    @Autowired
    private AdminMapper adminMapper;

    @Autowired
    private SysUserMapper sysUserMapper;

    @Autowired
    private JwtHelper jwtHelper;
    @Autowired
    private HttpServletRequest request;

    @Override
    public Result<String> addExam(AddExamForm addExamForm) {
        Exam exam = new Exam();
        BeanUtils.copyProperties(addExamForm, exam);
        this.save(exam);
        return Result.ok(null, "添加成功");
    }

    @Override
    public Result<String> updateExam(UpdateExamForm updatExamForm) {
        Exam one = this.getOne(new LambdaQueryWrapper<Exam>()
                .eq(Exam::getExamId, updatExamForm.getExamId()));
        if (one == null) {
            return Result.build(null, 500, "不存在该学期");
        }
        BeanUtils.copyProperties(updatExamForm, one);
        this.updateById(one);
        return Result.ok(null, "修改成功");
    }

    @Override
    public Result<String> deleteExam(Long id) {
        Exam one = this.getOne(new LambdaQueryWrapper<Exam>()
                .eq(Exam::getExamId, id));
        if (one == null) {
            return Result.build(null, 500, "不存在该学期");
        }
        this.removeById(one);
        return Result.ok(null, "删除成功");
    }

    @Override
    public PageResult<ExamVo> getExamPageByStu(ExamPageQuery examPageQuery) {
// 获取请求头中的令牌
        String token = request.getHeader("Authorization");
        Long userId = jwtHelper.getUserId(token);
        SysUser user = sysUserMapper.selectById(userId);
        if (user == null) {
            return PageResult.build(null, ResultCodeEnum.USER_ERROR);
        }
        Page<Course> page = new Page<>(examPageQuery.getPageNum(), examPageQuery.getPageSize());
        Student student = studentMapper.selectOne(new LambdaQueryWrapper<Student>()
                .eq(Student::getStudentId, Long.parseLong(user.getUsername())));
        if (student != null) {
            IPage<ExamVo> examIPage = examMapper.getMyCoursePageByStu(page, student.getStudentId(),examPageQuery);
            return PageResult.success(examIPage);
        }
        return PageResult.build(null, 500, "当前用户无权限");
    }

    @Override
    public PageResult<ExamVo> getExamPageByTea(ExamPageQuery examPageQuery) {
// 获取请求头中的令牌
        String token = request.getHeader("Authorization");
        Long userId = jwtHelper.getUserId(token);
        SysUser user = sysUserMapper.selectById(userId);
        if (user == null) {
            return PageResult.build(null, ResultCodeEnum.USER_ERROR);
        }
        Page<Course> page = new Page<>(examPageQuery.getPageNum(), examPageQuery.getPageSize());
        Teacher teacher = teacherMapper.selectOne(new LambdaQueryWrapper<Teacher>()
                .eq(Teacher::getTeacherId, Long.parseLong(user.getUsername())));
        if (teacher != null) {
            IPage<ExamVo> examIPage = examMapper.getMyCoursePageByTea(page,teacher.getTeacherId(),examPageQuery);
            return PageResult.success(examIPage);
        }
        return PageResult.build(null, 500, "当前用户无权限");
    }

    @Override
    public PageResult<ExamVo> getAllExamPage(ExamPageQuery examPageQuery) {
// 获取请求头中的令牌
        String token = request.getHeader("Authorization");
        Long userId = jwtHelper.getUserId(token);
        SysUser user = sysUserMapper.selectById(userId);
        if (user == null) {
            return PageResult.build(null, ResultCodeEnum.USER_ERROR);
        }
        Page<Course> page = new Page<>(examPageQuery.getPageNum(), examPageQuery.getPageSize());
        Admin admin = adminMapper.selectOne(new LambdaQueryWrapper<Admin>()
                .eq(Admin::getAdminId, Long.parseLong(user.getUsername())));
        if (admin != null) {
            IPage<ExamVo> examIPage = examMapper.getAllCoursePage(page,examPageQuery);
            return PageResult.success(examIPage);
        }
        return PageResult.build(null, 500, "当前用户无权限");
    }
}




